﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using IFRS.DataAccess.DAL;
using IFRS.Models;

namespace IFRS.DataAccess.DAL
{
    public class SPR_FCD
    {
        public DataTable SearchAll(FCD fcd, Paging paging, bool isAllBranch, string sCostCenter, ref int iRecordCount)
        {
            DataTable dt = new DataTable();
            IFRS_Utility Utility = new IFRS_Utility();
            List<SqlParameter> lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@RecordCount", SqlDbType.Int));
                lstParams[0].Direction = ParameterDirection.Output;
                lstParams.Add(new SqlParameter("@PageIndex", paging.PageIndex));
                lstParams.Add(new SqlParameter("@PageSize", paging.PageSize));
                lstParams.Add(new SqlParameter("@Sorting", paging.Sorting));

                lstParams.Add(new SqlParameter("@isAllBranch", isAllBranch.AsDBValue()));
                lstParams.Add(new SqlParameter("@sCostCenter", sCostCenter.AsDBValue()));

                lstParams.Add(new SqlParameter("@TransactionDate", fcd.TransactionDate));
                lstParams.Add(new SqlParameter("@AccountNo", fcd.AccountNo));
                lstParams.Add(new SqlParameter("@CostCenter", fcd.CostCenter));
                lstParams.Add(new SqlParameter("@IsConfirmed", fcd.IsConfirmed));
                dt = Utility.StoredProcDataTable("SPR_FCD_SEARCHALL", lstParams.ToArray());

                iRecordCount = (int)lstParams[0].Value;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return dt;
        }

        public DataTable SEARCH_BYID(int FCDID)
        {
            DataTable dt = new DataTable();
            IFRS_Utility Utility = new IFRS_Utility();
            List<SqlParameter> lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@FCDID", FCDID));
                dt = Utility.StoredProcDataTable("SPR_FCD_SEARCHBYID", lstParams.ToArray());
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return dt;
        }

        public int INSERT(FCD fcd)
        {
            int iNum = 0;
            IFRS_Utility Utility = new IFRS_Utility();
            List<SqlParameter> lstParams = new List<SqlParameter>();

            try
            {
                lstParams.Add(new SqlParameter("@FCDID", SqlDbType.Int));
                lstParams[0].Direction = ParameterDirection.Output;
                lstParams.Add(new SqlParameter("@TransactionDate", fcd.TransactionDate));
                lstParams.Add(new SqlParameter("@AccountNo", fcd.AccountNo));
                lstParams.Add(new SqlParameter("@GLAccount", fcd.GLAccount));
                lstParams.Add(new SqlParameter("@CostCenter", fcd.CostCenter));
                lstParams.Add(new SqlParameter("@StartedDate", fcd.StartedDate));
                lstParams.Add(new SqlParameter("@EndDate", fcd.EndDate));
                lstParams.Add(new SqlParameter("@CurrencyCode", fcd.CurrencyCode));
                lstParams.Add(new SqlParameter("@UnpaidCapital", fcd.UnpaidCapital));
                lstParams.Add(new SqlParameter("@InterestRate", fcd.InterestRate));
                lstParams.Add(new SqlParameter("@DayCountMethodID", fcd.DayCountMethodID));
                lstParams.Add(new SqlParameter("@InterestPaymentFreq", fcd.InterestPaymentFreq));
                lstParams.Add(new SqlParameter("@InterestPaymentFreqTimeBase", fcd.InterestPaymentFreqTimeBase));
                lstParams.Add(new SqlParameter("@PaymentConditionID", fcd.PaymentConditionID));
                lstParams.Add(new SqlParameter("@CustomerID", fcd.CustomerID));
                lstParams.Add(new SqlParameter("@IsConfirmed", fcd.IsConfirmed));
                lstParams.Add(new SqlParameter("@CreatedDate", fcd.CreatedDate));
                lstParams.Add(new SqlParameter("@CreatedBy", fcd.CreatedBy));
                lstParams.Add(new SqlParameter("@CreatedCostCenter", fcd.CreatedCostCenter));
                lstParams.Add(new SqlParameter("@CreatedBranch", fcd.CreatedBranch));
                lstParams.Add(new SqlParameter("@UpdatedDate", fcd.UpdatedDate));
                lstParams.Add(new SqlParameter("@UpdatedBy", fcd.UpdatedBy));
                lstParams.Add(new SqlParameter("@UpdatedCostCenter", fcd.UpdatedCostCenter));
                lstParams.Add(new SqlParameter("@UpdatedBranch", fcd.UpdatedBranch));

                Utility.ExecuteStoredProc("SPR_FCD_INSERT", lstParams.ToArray());
                iNum = (int)lstParams[0].Value;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return iNum;
        }

        public int UPDATE(FCD fcd)
        {
            int iNum = 0;
            IFRS_Utility Utility = new IFRS_Utility();
            List<SqlParameter> lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@FCDID", fcd.FCDID));
                lstParams.Add(new SqlParameter("@TransactionDate", fcd.TransactionDate));
                lstParams.Add(new SqlParameter("@AccountNo", fcd.AccountNo));
                lstParams.Add(new SqlParameter("@GLAccount", fcd.GLAccount));
                lstParams.Add(new SqlParameter("@CostCenter", fcd.CostCenter));
                lstParams.Add(new SqlParameter("@StartedDate", fcd.StartedDate));
                lstParams.Add(new SqlParameter("@EndDate", fcd.EndDate));
                lstParams.Add(new SqlParameter("@CurrencyCode", fcd.CurrencyCode));
                lstParams.Add(new SqlParameter("@UnpaidCapital", fcd.UnpaidCapital));
                lstParams.Add(new SqlParameter("@InterestRate", fcd.InterestRate));
                lstParams.Add(new SqlParameter("@DayCountMethodID", fcd.DayCountMethodID));
                lstParams.Add(new SqlParameter("@InterestPaymentFreq", fcd.InterestPaymentFreq));
                lstParams.Add(new SqlParameter("@InterestPaymentFreqTimeBase", fcd.InterestPaymentFreqTimeBase));
                lstParams.Add(new SqlParameter("@PaymentConditionID", fcd.PaymentConditionID));
                lstParams.Add(new SqlParameter("@CustomerID", fcd.CustomerID));
                lstParams.Add(new SqlParameter("@IsConfirmed", fcd.IsConfirmed));
                lstParams.Add(new SqlParameter("@UpdatedDate", fcd.UpdatedDate));
                lstParams.Add(new SqlParameter("@UpdatedBy", fcd.UpdatedBy));
                lstParams.Add(new SqlParameter("@UpdatedCostCenter", fcd.UpdatedCostCenter));
                lstParams.Add(new SqlParameter("@UpdatedBranch", fcd.UpdatedBranch));

                iNum = (int)Utility.ExecuteStoredProc("SPR_FCD_UPDATE", lstParams.ToArray());
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return iNum;
        }

        public int DELETE(FCD fcd)
        {
            int iNum = 0;
            IFRS_Utility Utility = new IFRS_Utility();
            List<SqlParameter> lstParams = new List<SqlParameter>();

            try
            {
                lstParams.Add(new SqlParameter("@FCDID", fcd.FCDID));
                lstParams.Add(new SqlParameter("@UpdatedDate", fcd.UpdatedDate));
                lstParams.Add(new SqlParameter("@UpdatedBy", fcd.UpdatedBy));
                lstParams.Add(new SqlParameter("@UpdatedCostCenter", fcd.UpdatedCostCenter));
                lstParams.Add(new SqlParameter("@UpdatedBranch", fcd.UpdatedBranch));
                iNum = (int)Utility.ExecuteStoredProc("SPR_FCD_DELETE", lstParams.ToArray());
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return iNum;
        }

    }
}
